package com.yhy;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class MyChannelInboundHandler1 extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //处理消息
        ByteBuf result = (ByteBuf)msg;
        byte[] content = new byte[result.readableBytes()-1];
        result.readBytes(content);

        String s = new String(content);

        System.out.println("hand1 channelRead: " + ctx.channel().remoteAddress() + s);
        ctx.fireChannelRead(msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("hand1 channelActive:"+ctx.channel().remoteAddress());
        ctx.fireChannelActive();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("hand1 channelReadComplete:"+ctx.channel().remoteAddress());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("hand1 channelInactive:"+ctx.channel().remoteAddress());
        ctx.fireChannelInactive();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("hand1 exceptionCaught:" + cause +ctx.channel().remoteAddress());
        ctx.fireExceptionCaught(cause);
    }
}
